<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
            <el-form-item :label="$t('vehicle.brand.vehicleBrand')" prop="vehicleBrand">
                <el-input v-model="queryParams.vehicleBrand" :placeholder="$t('vehicle.brand.placeholder.vehicleBrand')"
                    clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
                    $t('common.button.query') }}</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.button.reset')
                }}</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['vehicle:brand:add']">{{ $t('common.button.add') }}</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['vehicle:brand:edit']">{{ $t('common.button.edit') }}</el-button>
            </el-col> -->
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['vehicle:brand:remove']">{{ $t('common.button.remove') }}</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['vehicle:brand:export']">{{ $t('common.button.export') }}</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="brandList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column :label="$t('vehicle.brand.brandId')" align="center" prop="id" v-if="true" />
            <el-table-column :label="$t('vehicle.brand.vehicleBrand')" align="center" prop="vehicleBrand" />
            <el-table-column :label="$t('vehicle.brand.founder')" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="200">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column :label="$t('common.table.operate')" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['vehicle:brand:edit']">{{ $t('common.button.edit') }}</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['vehicle:brand:remove']">{{ $t('common.button.remove') }}</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 修改车辆品牌对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="modelForm" :model="model" :rules="modelRules" label-width="120px">
                <el-form-item :label="$t('vehicle.brand.brandId')" prop="id">
                    <el-input v-model="model.id" :disabled="true"
                        :placeholder="$t('vehicle.brand.placeholder.vehicleBrand')" />
                </el-form-item>
                <el-form-item :label="$t('vehicle.brand.vehicleBrand')" prop="vehicleBrand">
                    <el-input v-model="model.vehicleBrand" :placeholder="$t('vehicle.brand.placeholder.vehicleBrand')" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">{{ $t('common.dialog.confirm')
                }}</el-button>
                <el-button @click="cancel">{{ $t('common.dialog.cancel') }}</el-button>
            </div>
        </el-dialog>

        <!-- 新增 -->
        <el-dialog :title="title" :visible.sync="addOpen" width="700px" append-to-body @close="dialogClose">
            

            <el-form ref="modelForm" :model="model"  :rules="modelRules">
                <div v-for="(item, index) in  model.vehicleBrandList" :key="index" style="display: flex; align-items: center;">
                    <el-form-item
                        :label="$t('vehicle.brand.vehicleBrand')"
                        :prop="`vehicleBrandList.${index}.vehicleBrand`"
                        :rules="modelRules.vehicleBrand"
                        style="flex: 1;">
                        <el-input v-model="item.vehicleBrand" style="width: 250px;"
                            :placeholder="$t('vehicle.brand.placeholder.vehicleBrand')" />
                    </el-form-item>
                    <el-form-item>
                        <el-button v-if="index + 1 == model.vehicleBrandList.length" @click="addInput" type="primary">{{
                            $t('common.button.add') }}</el-button>
                        <el-button v-if="index !== 0" @click="deleteInput(item, index)" type="danger">{{
                            $t('common.button.remove') }}</el-button>
                    </el-form-item>
                </div>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">{{
                    $t('common.dialog.confirm') }}</el-button>
                <el-button @click="cancel">{{ $t('common.dialog.cancel') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listBrand, getBrand, delBrand, addBrand, updateBrand } from "@/api/vehicle/brand/data";
import { log } from "console";

export default {
    name: "Brand",
    data() {
        return {
            inputList: [{ vehicleBrand: "" }],
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 车辆品牌表格数据
            brandList: [],
            // 弹出层标题
            title: "",
            addOpen: false,
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                vehicleBrand: undefined,
            },
            // 表单参数
            form: {
                inputList: [{ vehicleBrand: "" }],
            },
            //测试表单参数
            model:{
                id:"",
                vehicleBrandList: [{ vehicleBrand: "" }],
                vehicleBrand:"",

            },
            // 表单校验

            modelRules:{
                vehicleBrand:[
                {
                          required: true,
                          message: this.$t('vehicle.brand.rules.brand'),
                          trigger: ['blur', 'change']
                        }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    mounted(){
        console.log(this.form.vehicleBrandList);
    },
    methods: {
        /** 查询车辆品牌列表 */
        getList() {
            this.loading = true;
            listBrand(this.queryParams).then(response => {
                this.brandList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.addOpen = false;
            // this.reset();
        },
        addInput() {
            this.model.vehicleBrandList.push({ vehicleBrand: '' });


            // this.$nextTick(() => {
            //     this.$refs.modelForm.validate();
            // });
        },
        deleteInput(item, index) {
            if (this.model.vehicleBrandList.length > 1) {
                this.model.vehicleBrandList.splice(index, 1);
                // this.$nextTick(() => {
                //     this.$refs.modelForm.validate();
                // });
            }
        },
        getValidationRules(index) {
            return {
                required: true,
                message: this.$t('vehicle.brand.rules.brand'),
                trigger: ['blur', 'change'],
                validator: (rule, value, callback) => {
                    // 校验逻辑，可以根据实际需求进行修改
                    if (!value) {
                        callback(new Error('请输入品牌'));
                    } else {
                        callback();
                    }
                }
            };
        },
        // 表单重置
        reset() {
            this.inputList = [{ vehicleBrand: "" }],
                this.form = {
                    id: undefined,
                    vehicleBrand: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.addOpen = true;
            this.title = this.$t('vehicle.brand.title.add');
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.loading = true;
            this.reset();
            const id = row.id || this.ids
            getBrand(id).then(response => {
                this.loading = false;
                this.model = response.data;
                this.open = true;
                this.title = this.$t('vehicle.brand.title.edit');
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["modelForm"].validate(valid => {
                console.log(valid);
                console.log(this.model);
                if (valid) {
                    this.buttonLoading = true;
                    if (this.model.id != null && this.model.id !="" && this.model.id != undefined) {
                        updateBrand(this.model).then(response => {
                            this.$modal.updateSuccess();
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        console.log("新增品牌");
                        addBrand(this.model).then(response => {
                            this.$modal.addSuccess();
                            this.addOpen = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm(this.$t('vehicle.brand.title.delete') + ids + this.$t('vehicle.brand.title.query')).then((res) => {
                this.loading = true;
                return delBrand(ids);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.$modal.deleteSuccess();
            }).catch(() => {
            }).finally(() => {
                this.loading = false;
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('vehicle/brand/export', {
                ...this.queryParams
            }, `brand_${new Date().getTime()}.xlsx`)
        },
        // 对话框关闭回调
        dialogClose(){
            this.$refs['modelForm'].resetFields();
            this.model.vehicleBrandList = [{ vehicleBrand: "" }];
        }
    }
};
</script>
